
import { notification, Spin } from 'antd';
import axios from 'axios'
export const baseURL = '/devapi/'

// 创建axios实例
const service = axios.create({
  baseURL, // api的base_url
  timeout: 10000,    // 请求超时时间
  withCredentials: true
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['token'] = 'token'
  config.headers['Content-Type'] = 'application/json;charset=UTF-8'
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    // console.log('response:', response)
    const data = response.data
    if (data.code !== 0) {
      // 接口报错
      notification.error({
        message: response.data.message,
      })
    }
    return response.data
  },
  error => {
    console.log('err' + error)
    if (error.response) {
      if (error.response.code === 500) {
        if (error.response.detail) {
          notification.error({
            message: error.response.detail,
          });
        } else {
          notification.error({
            message: "服务器错误",
          });
        }
      } else if (error.response.code === 504) {
        notification.error({
          message: "服务器连接失败",
        });
      }
    } else {
      notification.error({
        message: error.toString(),
      })
    }
    return Promise.reject(error)
  }
)
export default service
